package com.wandian.notepad.mvvm.notepadhome;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.view.GravityCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.TimeUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.notepad.R;
import com.wandian.notepad.adapter.NotepadGridRecycleAdapter;
import com.wandian.notepad.adapter.NotepadHistoryRecycleAdapter;
import com.wandian.notepad.bean.NoteTypeBean;
import com.wandian.notepad.databinding.NotepadActivityHomeBinding;
import com.wandian.notepad.mvvm.drawernotesfragment.DrawNotesFragment;

import q.rorbin.verticaltablayout.util.DisplayUtil;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 记事本首页activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NOTEPAD_HOME)
public class NotepadHomeActivity extends BaseMVVMActivity<NotepadHomeViewModel>  implements  BaseQuickAdapter.RequestLoadMoreListener{
    private NotepadActivityHomeBinding dataBinding;
    private int pageNumber = 1;
    private int pageSize = 30;
    private NotepadHistoryRecycleAdapter notepadHistoryRecycleAdapter;
    private NotepadGridRecycleAdapter notepadGridRecycleAdapter;
    private boolean isRefresh = false;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.notepad_activity_home);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initDrawLayout();
        initViewData();
        initRecycleView();
        initClink();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mViewModel.notepadTypeList();
        getData(true);
    }

    private void getData(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            notepadHistoryRecycleAdapter.setEnableLoadMore(true);
            pageNumber = 1;
            notepadHistoryRecycleAdapter.getData().clear();
            notepadHistoryRecycleAdapter.notifyDataSetChanged();
        }else {
            pageNumber = pageNumber+1;
        }
        mViewModel.notepadList(pageNumber+"",pageSize+"",dataBinding.refreshLayout,notepadHistoryRecycleAdapter);
    }

    private void initRecycleView(){
        //统计
        RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(getContext(),4);
        dataBinding.topRecycle.setLayoutManager(gridLayoutManager);
        notepadGridRecycleAdapter  = new NotepadGridRecycleAdapter(R.layout.notepad_item_grid_type);
        dataBinding.topRecycle.setAdapter(notepadGridRecycleAdapter);
        notepadGridRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                NoteTypeBean noteTypeBean = (NoteTypeBean)baseQuickAdapter.getData().get(i);
                if(!UIUtils.isEmpty(noteTypeBean)){
                    Bundle bundle = new Bundle();
                    bundle.putString("type_id",noteTypeBean.getId()+"");
                    bundle.putString("type_title",noteTypeBean.getType()+"");
                    ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NOTEPAD_CLASSIFY_LIST).with(bundle).navigation();
                }
            }
        });
        //列表
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.rvNotes.setLayoutManager(layoutManager);
        notepadHistoryRecycleAdapter  = new NotepadHistoryRecycleAdapter(R.layout.notepad_item_notes);
        dataBinding.rvNotes.setAdapter(notepadHistoryRecycleAdapter);
        notepadHistoryRecycleAdapter.setEmptyView(R.layout.empty_view,(ViewGroup) dataBinding.rvNotes.getParent());
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindSysTwoToolBar.toolBarTvTitle.setText("记事本");
        dataBinding.bindSysTwoToolBar.toolBarTitleRight.setText("管理");
        dataBinding.bindSysTwoToolBar.toolBarTitleRight.setVisibility(View.VISIBLE);

        dataBinding.refreshLayout.setColorSchemeResources(R.color.color_sys);
        dataBinding.refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });

        mViewModel.noteTypeList.observe(this,listBean->{
            if(!UIUtils.isEmpty(listBean) && listBean.size()>0){
                notepadGridRecycleAdapter.setNewData(listBean);
            }
        });

        mViewModel.noteList.observe(this,listBean->{
            if(!UIUtils.isEmpty(listBean) && listBean.size()>0){

                if(listBean.size() != pageSize) notepadHistoryRecycleAdapter.setEnableLoadMore(false);
                if(isRefresh){
                    notepadHistoryRecycleAdapter.setNewData(listBean);
                }else {
                    notepadHistoryRecycleAdapter.addData(listBean);
                    notepadHistoryRecycleAdapter.loadMoreComplete();
                }
            }
        });
    }

    private void initDrawLayout(){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        DrawNotesFragment fragment = new DrawNotesFragment();
        ft.replace(R.id.fl_container_drawer, fragment);
        ft.commitAllowingStateLoss();
    }

    private void initClink(){
        dataBinding.bindSysTwoToolBar.toolBarIvBack.setOnClickListener(v -> finish());

        //更多
        dataBinding.bindSysTwoToolBar.toolBarTitleRight.setOnClickListener(v ->  dataBinding.drawerLayout.openDrawer(GravityCompat.START));

        //添加记事
        dataBinding.tvAddNotes.setOnClickListener(v ->  ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NOTEPAD_ADD).navigation());
    }

    @Override
    public void onLoadMoreRequested() {
        getData(false);
    }
}
